import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import '../widget/myDialog.dart';

class DialogPage extends StatefulWidget {
  const DialogPage({super.key});

  @override
  State<DialogPage> createState() => _DialogPageState();
}

class _DialogPageState extends State<DialogPage> {
  void _alertDialog() async {
    var result = await showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text("提示信息"),
          content: Text("您确定要删除吗？"),
          actions: [
            TextButton(
              onPressed: () {
                print("Ok");
                Navigator.of(context).pop('确定');
              },
              child: Text("确定"),
            ),
            TextButton(
              onPressed: () {
                print("Cancel");
                Navigator.of(context).pop('取消');
              },
              child: Text("取消"),
            ),
          ],
        );
      },
    );
    print(result);
  }

  void _simpleDialog() async {
    var result = await showDialog(
      context: context,
      barrierDismissible: false, // 表示点击灰色背景的时候是否消失弹出框
      builder: (context) {
        return SimpleDialog(
          title: Text("请选择语言"),
          children: [
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, "汉语");
              },
              child: Text("汉语"),
            ),
            Divider(),
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, "英语");
              },
              child: Text("英语"),
            ),
            Divider(),
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, "韩语");
              },
              child: Text("韩语"),
            ),
          ],
        );
      },
    );
    print("选择的语言：" + result);
  }

  void _modelBottomSheet() async {
    var result = await showModalBottomSheet(
      context: context,
      builder: (context) {
        return Container(
          height: 240,
          child: Column(
            children: [
              ListTile(
                title: Text("分享"),
                onTap: () {
                  Navigator.pop(context, "分享");
                },
              ),
              Divider(),
              ListTile(
                  title: Text("收藏"),
                  onTap: () {
                    Navigator.pop(context, "收藏");
                  }),
              Divider(),
              ListTile(
                  title: Text("取消"),
                  onTap: () {
                    Navigator.pop(context, "取消");
                  }),
            ],
          ),
        );
      },
    );
    print('选择的是' + result);
  }

  void _toast() {
    print("_toast");
    Fluttertoast.showToast(
      msg: "提示信息",
      toastLength: Toast.LENGTH_SHORT, // 提示时间  针对Android
      gravity: ToastGravity.CENTER, // 方位
      timeInSecForIosWeb: 1, // 提示时间  针对ios和web
      backgroundColor: Colors.black, // 背景颜色
      textColor: Colors.white, // 文本颜色
      fontSize: 16.0, // 字体大小
    );
  }

  void _myDialog() async {
    var result = await showDialog(
      context: context,
      barrierDismissible: false, // 表示点击灰色背景的时候是否消失弹出框
      builder: (context) {
        return MyDialog(
          title: "提示！",
          content: "我是一个内容",
          onTap: () {
            Navigator.pop(context,"我是自定义dialog关闭的按钮事件");
          },
        );
      },
    );
    print(result);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Dialog"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: _alertDialog,
              child: Text("alert弹出框-AlertDialog"),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _simpleDialog,
              child: Text("select弹出框-SimpleDialog"),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _modelBottomSheet,
              child: Text("ActionSheet弹出框-showModalBottomSheet"),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _toast,
              child: Text("Toast"),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _myDialog,
              child: Text("自定义dialog"),
            ),
          ],
        ),
      ),
    );
  }
}
